package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuDataVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lfy
 * @description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @createDate 2023-07-11 15:27:09
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;


    @Transactional
    @Override
    public void saveSpuInfo(SpuDataVo dataVo) {
        //1、基本信息保存到 spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(dataVo, spuInfo);
        this.save(spuInfo);


        //保存成功的自增id，后面要用
        Long spuId = spuInfo.getId();


        //2、所有图片保存到 spu_image
        List<SpuImage> spuImages = dataVo.getSpuImageList()
                .stream()
                .map(item -> {
                    SpuImage image = new SpuImage();
                    BeanUtils.copyProperties(item, image);
                    //回填spuId
                    image.setSpuId(spuId);
                    return image;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImages);

        //3、这个spu的所有销售属性名保存到 spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrs = dataVo.getSpuSaleAttrList()
                .stream()
                .map(item -> { //单值映射
                    SpuSaleAttr saleAttr = new SpuSaleAttr();
                    //转换 & 回填spuId
                    BeanUtils.copyProperties(item, saleAttr);
                    saleAttr.setSpuId(spuId);
                    return saleAttr;
                }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrs);


        //4、这个spu的某个销售属性的所有值保存到 spu_sale_attr_value
        List<SpuSaleAttrValue> saleAttrValues = dataVo.getSpuSaleAttrList()
                .stream() //循环 属性名
                .flatMap(item -> {  //多值映射：把每一个属性的所有属性值都收集过来
                    //item：第一层的元素，销售属性名
                    //拿到每一个销售属性名的所有销售属性值
                    Stream<SpuSaleAttrValue> valueStream = item.getSpuSaleAttrValueList()
                            .stream()
                            .map(subitem -> {
                                //subitem：第二层，销售属性值
                                SpuSaleAttrValue value = new SpuSaleAttrValue();
                                value.setSpuId(spuId); //回填
                                value.setBaseSaleAttrId(item.getBaseSaleAttrId()); //销售属性id
                                value.setSaleAttrName(item.getSaleAttrName()); //销售属性名
                                value.setSaleAttrValueName(subitem.getSaleAttrValueName()); //销售属性值

                                return value;
                            });
                    return valueStream;
                }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(saleAttrValues);
    }
}




